-- tests scene
local layer = nil
local scene = nil

local Btn_Tips  = 0x01
local Btn_Item1 = 0x02
local Btn_Item2 = 0x03
local Btn_Item3 = 0x04

local _lastTips = 0
local _TipsTimes = 5 --提示间隔(秒,久未找到砖块时提示

local function initWithLayer(stage)
    local _func_onShowResult = nil
    local _gameComplete = nil
    local _on_result = false
    
    local _lastCombo = 0
    local _iCombo = 1
    
    local _btnItem_AutoSl= nil
    local _btnItem_Time = nil
    local _btnItem_Baozou = nil
    
    _lastTips = 0
    
    
    local _StageInfos = StageInfos_HLWS[stage]
    local gemType = _StageInfos[PGemType]
    
    -- 暴走模式开启时机
    local _baozoutime = 0
    local _onbaozou = false
    -- 当前关卡
    local _stage = stage
    -- 总分数
    local _gameScore = 0
    -- 魂力值
    local _gameSoulPer= 0
    -- 最高连击数
    local _gameCombo = 0
    
    
    -- 本局总时间
    local _PlayGameTime = GPlayGameTime
    -- 停止掉的时间(时间停止道具
    local _PlayGameStopTime = 0
    -- 剩余需要停止的时间
    local _PlayGameStopLastTime = 0
    -- 无限暴走
    local _OnBaoForever = false
    -- 游戏时间
    local _gameRunTime = 0
    
    local _firstEnd = true
    
    -- 提示框x2
    local _tipslayer = CCLayer:create()
    local _btNor1 = CCSprite:createWithSpriteFrameName("TEBrick_Pick_01.png")
    local _btNor2 = CCSprite:createWithSpriteFrameName("TEBrick_Pick_01.png")
    _tipslayer:addChild(_btNor1)
    _tipslayer:addChild(_btNor2)
    scene:addChild(_tipslayer, 4)
    _btNor1:setVisible(false)
    _btNor2:setVisible(false)

    
    -- 每次结束后都会判定得到可交换列表
    local _swaplist = {}
    local function TestSwap(gem1, gem2)
        if gem1 ~= nil and gem2 ~= nil then
            has,rvalue = has_Puz_Gems(_GGemTable, gem1, gem2)
            return has
        end
        return false
    end
    
    -- 获取可排列的宝石列表
    local function getSwapList(bend)
        _swaplist = {}
        -- 每次尝试与右边/下边的宝石交换,看是否能交换成功
         for r=0,Gems_Rows-2 do
            for c=0,Gems_Cols-2 do
                local cgem = _GGemTable[getIndex(r,c)]
                if cgem ~= nil then
                    --右边
                    local cgemr = _GGemTable[getIndex(r,c+1)]
                    local cgemd = _GGemTable[getIndex(r+1,c)]
                    local has = false
                    local cgems = nil
                    if TestSwap(cgem, cgemr) then
                        cgems = cgemr
                        has = true
                    elseif TestSwap(cgem, cgemd) then
                        cgems = cgemd
                        has = true
                    end
                    
                    if has then
                        --- 可以交换
                        --cgem.img:setColor(ccc3(255, 0, 0))
                        --cgems.img:setColor(ccc3(255, 0, 0))
                        --return
                        table.insert(_swaplist, {g1=cgem, g2=cgems})
                        
                        if bend then
                            return
                        end
                    end
                end
            end
        end
    end

    local _onNewRoundLeat = nil
    local _onShowTips = false
    -- 点击提示按钮
    local function BtnTipsCallback()
        getSwapList()
        
        _lastTips = 0
        _onShowTips = true
        
        if _deleteSums == 0 and _moveSums == 0 and #_swaplist > 0 then
            --随机一个
            local idx = math.random(1,#_swaplist)
            local tipsItem = _swaplist[idx]
            
            _btNor1:setVisible(true)
            _btNor2:setVisible(true)
            local px,py = tipsItem.g1:GetPosition()
            _btNor1:setPosition(ccp(px,py))
            px,py = tipsItem.g2:GetPosition()
            _btNor2:setPosition(ccp(px,py))
            
            local arr1 = CCArray:create()
            arr1:addObject(CCFadeTo:create(0.5, 100))
            arr1:addObject(CCFadeTo:create(0.5, 255))
            _btNor1:stopAllActions()
            _btNor1:runAction(CCRepeatForever:create(CCSequence:create(arr1)))
            
            
            local arr1 = CCArray:create()
            arr1:addObject(CCFadeTo:create(0.5, 100))
            arr1:addObject(CCFadeTo:create(0.5, 255))
            _btNor2:stopAllActions()
            _btNor2:runAction(CCRepeatForever:create(CCSequence:create(arr1)))
        end
    end
    
    local function clearGemsTips()
        _lastTips = 0
        _onShowTips = false
    end
    
    
    local removeErrorBrick = function()
        --local sums = 0
        --table.foreach(_GGemTableKeys, function(k, fgem)
        --    tgem = _GGemTable[getIndex(fgem.row,fgem.col)]
        --    if tgem ~= nil and tgem.key ~= fgem.key then
        --        _func_deleteGem(fgem, true)
        --    end
        --    sums = sums + 1
        --end)
        --sums = 0
        --table.foreach(_GGemTable, function(k, fgem)
        --    sums = sums + 1
        --end)
    end
    
    
    
    ---- 重新判定宝石排列
    local _timerIDNew = nil
    _onNewRoundLeat = function()
        --
        if _timerIDNew ~= nil then
            CCDirector:sharedDirector():getScheduler():unscheduleScriptEntry(_timerIDNew)
            _timerIDNew = nil
        end
        
        --removeErrorBrick()
        --print("_swaplist")
        
        getSwapList(true)
        if #_swaplist <= 0 then
            --如果没有可排列的则重新生成
            _func_deleteAllGems()
            return false
        end
        
        return true
    end
    
    -- 点击交换
    local function onSwap(gem1, gem2)
        -- 隐藏交换指示
        _btNor1:setVisible(false)
        _btNor2:setVisible(false)
    end

    -- 按钮回调函数
    local function menuCallback(tag)
        if tag == Btn_Tips then
            --BtnTipsCallback()
        elseif tag == Btn_Item1 then
            --local irow = math.random(0, Gems_Rows-1)
            --local icol = math.random(0, Gems_Cols-1)
            --local typ = Item_BomArce
            --_func_changeToBom(_GGemTable[getIndex(irow,icol)], typ)
        elseif tag == Btn_Item2 then
            --local irow = math.random(0, Gems_Rows-1)
            --local icol = math.random(0, Gems_Cols-1)
            --local typ = Item_BomCorss
            --_func_changeToBom(_GGemTable[getIndex(irow,icol)], typ)
        elseif tag == Btn_Item3 then
            --local typ = Item_BomRandom
            --local irow = math.random(0, Gems_Rows-1)
            --local icol = math.random(0, Gems_Cols-1)
            --_func_changeToBom(_GGemTable[getIndex(irow,icol)], typ)
        end
        
        Play_Effect(sme_Click)
    end
    
    -- 生成没有三连的宝石阵
    function createAllGems()
        --local tempTable = {}
        
        for r = 0, Gems_Rows-1 do
            local c = 0
            while c < Gems_Cols do
                local cgem = createGems(r,c)
                --tempTable[getIndex(r,c)] = cgem
				--print("createAllGems",cgem.tpe, r, c)
                has, rvlist = has_Puz_Gem(_GGemTable, cgem.tpe, r, c)
                --如果新宝石可以形成消除,则重新生成
                if not has then
                    c = c + 1
                else
                    _func_deleteGem(cgem, true)
                    --print("重新生成")
                end
            end
        end
    end
    
    -------- ui
    --背景
    local scfile = ""
    local scbfile = ""
    if Scene_Flat == _StageInfos[PScene] then
        scfile = "FB_py.png"
        scbfile = "FBG_py.png"
    elseif Scene_Forest == _StageInfos[PScene] then
        scfile = "FB_ss.png"
        scbfile = "FBG_ss.png"
    elseif Scene_Seaside == _StageInfos[PScene] then
        scfile = "FB_hb.png"
        scbfile = "FBG_hb.png"
    elseif Scene_Castle == _StageInfos[PScene] then
        scfile = "FB_cb.png"
        scbfile = "FBG_cb.png"
    end
    
    
    local _bgBrick = CCSprite:createWithSpriteFrameName(scfile)
    _bgBrick:setAnchorPoint(ccp(0, 0))
    _bgBrick:setPosition(gdccp(0, 0))
    layer:addChild(_bgBrick, 1)
    
    -- 背景2
    local _bgpx = 0
    local _bgpy = 133
    local _bg2px = 0
    local _bg2py = 0
    
    local _bgBrick = CCSprite:createWithSpriteFrameName(s_ShoujiBg)
    _bgBrick:setAnchorPoint(ccp(0, 0))
    _bgBrick:setPosition(gdccp(_bgpx, _bgpy))
    layer:addChild(_bgBrick, 1)
    -- 背景2
    local _bgBrick2 = CCSprite:createWithSpriteFrameName(s_ShoujiBg2)
    _bgBrick2:setAnchorPoint(ccp(0, 0))
    _bgBrick2:setPosition(gdccp(_bg2px, _bg2py))
    layer:addChild(_bgBrick2, 2)
    
    --- 进度条
    --- 背景
    _bgpx = 38
    _bgpy = DWinSizeHeight - 150
    local _GameProgressBG = CCSprite:createWithSpriteFrameName("HLWS_T0.png")
    _GameProgressBG:setAnchorPoint(ccp(0, 0.5))
    _GameProgressBG:setPosition(gdccp(_bgpx, _bgpy))
    layer:addChild(_GameProgressBG, 2)
    
    local _GameProgress = CCProgressTimer:create(CCSprite:createWithSpriteFrameName("HLWS_T1.png"))
    _GameProgress:setType(kCCProgressTimerTypeBar)
    _GameProgress:setAnchorPoint(ccp(0, 0.5))
    _GameProgress:setMidpoint(CCPointMake(0, 0))
    _GameProgress:setBarChangeRate(CCPointMake(1, 0))
    _GameProgress:setPosition(gdccp(_bgpx, _bgpy))
    layer:addChild(_GameProgress,2)

    -- 分数
    local _gui_jf = CCSprite:createWithSpriteFrameName("GUI_SC.png")
    _gui_jf:setPosition(gdccp(120, DWinSizeHeight-22))
    layer:addChild(_gui_jf, LayerIdx_Gui)
    
    local _lblScore = GameNumbers:new(vNumberScore, NumberAlginment_Left, nil, 7, true)
    _lblScore:addLayer(layer, LayerIdx_Gui)
    _lblScore:setNumber(0)
    _lblScore:setPosition(gdccp(150, DWinSizeHeight-27))
    
    -- 魂力
    local _gui_hl = CCSprite:createWithSpriteFrameName("Huili.png")
    _gui_hl:setPosition(gdccp(120, DWinSizeHeight-70))
    layer:addChild(_gui_hl, LayerIdx_Gui)
    
    local _lblSoul = GameNumbers:new(vNumberScore, NumberAlginment_Left, nil, 7, true)
    _lblSoul:addLayer(layer, LayerIdx_Gui)
    _lblSoul:setNumber(0)
    _lblSoul:setPosition(gdccp(150, DWinSizeHeight-73))
    
    -- 时间
    local _gui_hl = CCSprite:createWithSpriteFrameName("GUI_time.png")
    _gui_hl:setPosition(gdccp(450, DWinSizeHeight-40))
    layer:addChild(_gui_hl, LayerIdx_Gui)
    
    local _lblTime = GameNumbers:new(vNumberTime)
    _lblTime:addLayer(layer, LayerIdx_Gui)
    _lblTime:setNumber("0")
    _lblTime:setPosition(gdccp(540, DWinSizeHeight-42))
    
    
    local function updateItemsCount()
        _btnItem_Time:updateCut(GetItems(Item_TimerUp))
        _btnItem_AutoSl:updateCut(GetItems(Item_AutoSl))
        _btnItem_Baozou:updateCut(GetItems(Item_Baozou))
    end
    
    -- 创建一个主菜单
    local _Menu = CCMenu:create()
    
    bpx = 150
    bpy = 60
    binvw = 80
    
    local function menuCallbackItem(tag)
        --print("items",tag)
        if tag == Item_TimerUp then
            
            if UseItems(Item_TimerUp) then
                _firstEnd = true
                -- 直接增加游戏时间(20秒
                _PlayGameTime = _PlayGameTime + GItmeP_TimeUp
            end
        elseif tag == Item_TimerStop then
            if UseItems(Item_TimerStop) then
                _PlayGameStopTime = _PlayGameStopTime + GItemP_TimeStop
                _PlayGameStopLastTime = _PlayGameStopLastTime + GItemP_TimeStop
            end
        elseif tag == Item_Baozou then
            if not _OnBaoForever then
                if UseItems(Item_Baozou) then
                    _OnBaoForever = true
                    GFunc_ShowBaozouEffs()
                end
            else
                Play_Effect(sme_ClickError)
            end
        elseif tag == Item_AutoSl then
            local irow = Gems_Rows-1
            local icol = 0
            local cgemss = _GGemTable[getIndex(irow,icol)]
            if cgemss and UseItems(Item_AutoSl) then
                _func_UseToBom(cgemss, Item_Bom)
            end
        end
        
        --Play_Effect(sme_Click)
        
        updateItemsCount()
    end
    
    
    local invW = 200
    local invSums = 3
    local bpx = DWinSizeWidth/2 + (1-invSums)*invW/2
    local bpy = 60
    local index = 0
    
    local layerItems = CCLayer:create()
    layer:addChild(layerItems, LayerIdx_Gui)
    
    _btnItem_AutoSl = GameItems:new(Item_AutoSl, menuCallbackItem)
    _btnItem_AutoSl:setPosition(bpx + invW * index, bpy)
    _btnItem_AutoSl:addToChild(layerItems)
    index = index + 1
    
    _btnItem_Time = GameItems:new(Item_TimerUp, menuCallbackItem)
    _btnItem_Time:setPosition(bpx + invW * index, bpy)
    _btnItem_Time:addToChild(layerItems)
    index = index + 1
    
    _btnItem_Baozou = GameItems:new(Item_Baozou, menuCallbackItem)
    _btnItem_Baozou:setPosition(bpx + invW * index, bpy)
    _btnItem_Baozou:addToChild(layerItems)
    
    updateItemsCount()
    
    
    -- 主菜单大小
    _Menu:setContentSize(CCSizeMake(DWinSizeWidth,  DWinSizeHeight))
    -- 设置坐标
    _Menu:setPosition(0, 0)
    -- 添加至主菜单层
    layer:addChild(_Menu, LayerIdx_Gui)
    
    
    -------------------------
    if _StageInfos[PGTime] then
        _PlayGameTime = _StageInfos[PGTime]
    end
    --_PlayGameTime = _StageInfos[PGTime]
    
    
    
    
    ---- 显示游戏结果
    _func_onShowResult = function(bwin)
        if _on_result then
            return
        end
        _on_result = true
        
        --GamePasueHelper()
        _GameEndRun = true
        
        -- 继续/下一关
        local function onNextStage()
            Create_HLWS_SceneRun(_stage + 1)
            
            Play_Effect(sme_Click)
        end
        
        -- 重新开始
        local function onRestStage()
            Create_HLWS_SceneRun(_stage)
            
            Play_Effect(sme_Click)
        end
    
        -- 关闭暴走效果
        Gfunc_DelBazouEffs()
        
        ------- 显示结果
        local layerRestul = CCLayerColor:create(ccc4(0,0,0,80))
        local ResultMainMenu = CCMenu:create()
        -- 主菜单大小
        ResultMainMenu:setContentSize(CCSizeMake(DWinSizeWidth,  DWinSizeHeight))
        -- 设置坐标
        ResultMainMenu:setPosition(0, 0)
        -- 添加至主菜单层
        layerRestul:addChild(ResultMainMenu, 5)
        layer:addChild(layerRestul, 5)
        
        
        local btnItem = nil
        btnItem = CCMenuItemLabel:create(CCLabelTTF:create("", "Arial", FontSize))
        btnItem:setPosition(gdccp(DWinSizeWidth/2, DWinSizeHeight/2))
        btnItem:setContentSize(CCSizeMake(DWinSizeWidth,  DWinSizeHeight))
        ResultMainMenu:addChild(btnItem,100)
        
        local _widthb = 94
        local _heightb = 32
        local btnSpriteN = nil
        local btnSpriteS = nil
        local btnItem = nil
        
        -- 最后一关之前
        local invW = 200
        local invSums = 2
        local bpx = DWinSizeWidth/2 + (1-invSums)*invW/2
        local bpy = DWinSizeHeight/2 - 80
        local index = 0
        
        local restbg = s_Game_WinImgBG
        local restlg = s_Game_WinImg
        
        if bwin then
            _gameScore = _StageInfos[PScore]
            _lblScore:setNumber(_gameScore)
            
            --胜利音乐
            Play_BackgroundMusic(sm_Win)
            -- 最后一关之前
            invW = 200
            invSums = 2
            bpx = DWinSizeWidth/2 + (1-invSums)*invW/2
            index = 0
            --最后一关前显示下一关
            if _stage < #Stage_Infos_ZJWG - 1 then
                invSums = 3
                bpx = DWinSizeWidth/2 + (1-invSums)*invW/2
                
                local btnItem = GFunc_CreateButtonP("rst_next.png", onNextStage)
                btnItem:setPosition(gdccp(bpx + invW * index, bpy))
                ResultMainMenu:addChild(btnItem)
                index = index + 1
            else
                --如果是最后一关,成就通过
                Func_DataStatis(DS_ModeEnd, GameMode_HLWS)
            end
            
            local btnItem = GFunc_CreateButtonP("rst_rest.png", onRestStage)
            btnItem:setPosition(gdccp(bpx + invW * index, bpy))
            ResultMainMenu:addChild(btnItem)
            index = index + 1
            
            local btnItem = GFunc_CreateButtonP("fh_return.png", MainMenuCallback)
            btnItem:setPosition(gdccp(bpx + invW * index, bpy))
            ResultMainMenu:addChild(btnItem)
        else
            Play_BackgroundMusic(sm_Lost)
            index = 0
            
            local btnItem = GFunc_CreateButtonP("rst_rest.png", onRestStage)
            btnItem:setPosition(gdccp(bpx + invW * index, bpy))
            ResultMainMenu:addChild(btnItem)
            index = index + 1
            
            local btnItem = GFunc_CreateButtonP("fh_return.png", MainMenuCallback)
            btnItem:registerScriptTapHandler(MainMenuCallback)
            btnItem:setPosition(gdccp(bpx + invW * index, bpy))
            ResultMainMenu:addChild(btnItem)
            ---- 如果购买了时间道具,则增加"增加时间"按钮的显示
            
            restbg = s_Game_LostImgBG
            restlg = s_Game_LostImg
        end
        
        -- 结果背景
        _bgSpr = CCSprite:createWithSpriteFrameName(restbg)
        _bgSpr:setPosition(gdccp(DWinSizeWidth/2, DWinSizeHeight/2))
        layerRestul:addChild(_bgSpr)
        _bgSpr = CCSprite:createWithSpriteFrameName(restlg)
        _bgSpr:setPosition(gdccp(DWinSizeWidth/2, DWinSizeHeight/2+150))
        layerRestul:addChild(_bgSpr)
        
        --显示魂力值与分数
        local spriteHL = CCSprite:createWithSpriteFrameName("rst_score.png")
        spriteHL:setPosition(gdccp(DWinSizeWidth/2 - 150, DWinSizeHeight/2+60))
        layerRestul:addChild(spriteHL)
        
        local txthl = CCLabelTTF:create(_gameScore, "Arial", FontSize4)
        txthl:setPosition(gdccp(DWinSizeWidth/2 + 50, DWinSizeHeight/2+60))
        layerRestul:addChild(txthl)
        
        spriteHL = CCSprite:createWithSpriteFrameName("rst_hunli.png")
        spriteHL:setPosition(gdccp(DWinSizeWidth/2 - 150, DWinSizeHeight/2))
        layerRestul:addChild(spriteHL)
        
        txthl = CCLabelTTF:create(_gameSoulPer, "Arial", FontSize4)
        txthl:setPosition(gdccp(DWinSizeWidth/2 + 50, DWinSizeHeight/2))
        layerRestul:addChild(txthl)
        
        --_gameSoulPer
        --_gameScore
        Func_DataStatis(DS_Score, _gameScore)
    end
    
    
    --------------------- 游戏事件回调
    _gameComplete = function()
        -- 计算判定是否过关
        local ld_score = GetStageDatas(_stage)
        -- 魂力增加到总魂力值
        AddSoulPower(_gameSoulPer);
        -- 分数更新当前关卡
        if _gameScore > ld_score then
            UpdateStageDatas(_stage, _gameScore)
        end
        --print(_gameScore)
        if _StageInfos[PScore] <= _gameScore then
            _func_onShowResult(true)
        else
            _func_onShowResult(false)
        end
    end
    local function gameAddScene(v)
        --print("gameAddScene",v)
    end
    local function gameCombo(icombo, gemsums, bomgem)
        
        --连击数
        --_iCombo = _iCombo + 1
        if _lastCombo < GHLWSComboFps then
            _iCombo = _iCombo + 1
        else
            _iCombo = 1
        end
        _lastCombo = 0
        
        --显示连击数
        if bomgem~=nil then
            GFunc_ComboEffs(_iCombo, ccp(bomgem:GetPosition()))
        end
        
        -- 提示解除
        clearGemsTips()
        
        -- 计算分数
        -- 分数=(消除个数+2) * 10
        -- 魂力值=消除个数+2
        -- 得分=分数*连击数
        local score = (gemsums + 2) * 10
        local soulper = (gemsums + 2)
        Func_DataStatis(DS_HunJing, gemsums)
        
        -- 如果连击数大于8,则分数倍数=16
        local scoreCombo = _iCombo
        -- 如果连击数大于8,则魂力值倍数=12
        local soulperCombo = _iCombo
        if _iCombo >= 8 and _onbaozou == false then
            _baozoutime = 0
            _onbaozou = true
            -- 暴走模式已经开启
            Func_DataStatis(DS_Baozou)
            
            GFunc_ShowBaozouEffs()
        end
        if _onbaozou or _OnBaoForever then
            scoreCombo = 16
            soulperCombo = 12
        end
        --暴走模式下,每次连击增加暴走时间
        if _onbaozou then
            _baozoutime = _baozoutime - 0.2
        end
        
        local addScore = score * scoreCombo
        --13/3/22
        --local addSoulper = soulper * soulperCombo
        local addSoulper = gemsums
        
        
        -- 计分
        _gameScore = _gameScore + addScore
        _gameSoulPer= _gameSoulPer + addSoulper
        if _iCombo > _gameCombo then
            _gameCombo = _iCombo
            Func_DataStatis(DS_Combo, _gameCombo)
        end
        
        _lblSoul:setNumber(_gameSoulPer)
        _lblScore:setNumber(_gameScore)
        
        --- 更新进度条
        if _StageInfos[PScore] <= _gameScore then
            _GameProgress:setPercentage(100)
            _gameComplete()
        else
            _GameProgress:setPercentage(_gameScore/_StageInfos[PScore] * 100)
        end
    end
    local function gameKills(v)
    end
    local function onHurt(v)
    end
    local function onTimer(v,dt)
        
        if _GameFirst then
            return
        end
        
        _gameRunTime = _gameRunTime + dt
        local ftime = _PlayGameTime + _PlayGameStopTime - _gameRunTime
        
        -- 快结束时音效
        if _firstEnd and ftime <= 12 then
            Play_Effect(sme_GameToEnd)
            _firstEnd = false
        end
        
        if ftime <= 0 then
            -- 游戏结束
            _gameComplete()
            return
        end
            
        _lastCombo = _lastCombo + dt
            
        
        --暂停,减掉暂停时间
        if _PlayGameStopLastTime ~= 0 then
            _PlayGameStopLastTime = _PlayGameStopLastTime - dt
            if _PlayGameStopLastTime <= 0 then
                _PlayGameStopLastTime = 0
            end
        else
            --_lblTime:setNumber(GFunc_ChangeToTimeFormat(ftime))
            _lblTime:setNumber(math.floor(ftime))
            if _onbaozou then
                _baozoutime = _baozoutime + dt
                --- 暴走计时
                if _baozoutime > GBaoZouTimer then
                    _onbaozou = false
                    if not _OnBaoForever then
                        Gfunc_DelBazouEffs()
                    end
                end
            end
        end
        
        --提示
        _lastTips = _lastTips + dt
        if not _onShowTips and _lastTips >= _TipsTimes then
            BtnTipsCallback()
        end
    end
    
    --- 新回合的回调
    local function onNewRound()
        removeErrorBrick()
        -- 延迟判定当前宝石阵是否可排列（防卡
        _timerIDNew = CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(_onNewRoundLeat,0.1, false)
        
        
        --
        --_iCombo = 0
    end
    
    local gamefunc = {
        [PFunc_Complete] = _gameComplete,
        [PFunc_AddScore] = gameAddScene,
        [PFunc_Combo] = gameCombo,
        [PFunc_Kills] = gameKills,
        [PFunc_Hurts] = onHurt,
        [PFunc_Timer] = onTimer,
        [PFunc_Swap] = onSwap,
        [PFunc_newRound] = onNewRound,
    }
    
    -- 三消游戏UI
    layer:addChild(Create_Game_Scene(GameMode_Puz, gemType, PlayMode_ShouJi, gamefunc), 3)
    
    
    
    --显示教程
    Func_ShowGameTutorial(UI_HLWS_Game, function()
        -- 生成宝石阵
        createAllGems()
        while true do
            if _onNewRoundLeat() then
                break
            end
        end
        
        Func_BeginResFills()
        
        GFunc_CreateShowMessage("通关条件", string.format("积分达到 %d。", _StageInfos[PScore]), false, scene)
    end, scene)
    
    return layer
end

function Create_HLWS_SceneRun(stage)
    scene = CCScene:create()
    layer = CCLayer:create()
    scene:addChild(layer)

    initWithLayer(stage)
    scene:addChild(CreateGameStopLayer(ccp(50, DWinSizeHeight-50)))
    
    GameSetScne(scene, UI_HLWS_Game)
end

function Create_HLWS_Scene(stage)
    LoadingTutorial(UI_HLWS_Game, Create_HLWS_SceneRun, stage)
end
